package cn.ecut.reflection;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @Author ruanxin
 */
public class Test05 {
    public static void main(String[] args) throws Exception {
        User user=new User();
        Class c1 = user.getClass();

        //获取类名
        System.out.println(c1.getName());
        System.out.println(c1.getSimpleName());

        //获取属性
        System.out.println("=======================================");
        Field[] fields = c1.getFields();//只能找到public属性
        for (Field field : fields) {
            System.out.println(field);
        }
        System.out.println("=======================================");
        Field[] declaredFields = c1.getDeclaredFields();//找到所有属性
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);
        }
        System.out.println("=======================================");
        Field name = c1.getField("name");  //找到指定属性(public)
        System.out.println(name);

        //获得方法
        System.out.println("=======================================");
        Method[] methods = c1.getMethods();     //获取本类及其父类的所有public方法
        for (Method method : methods) {
            System.out.println("正常的"+method);
        }
        System.out.println("=======================================");
        methods = c1.getDeclaredMethods();      //获取本类声明的所有方法
        for (Method method : methods) {
            System.out.println("声明的"+method);
        }

        //获得指定方法
        System.out.println("=======================================");
        Method getName = c1.getMethod("getName", null);
        Method setName = c1.getMethod("setName", String.class);
        System.out.println(getName);
        System.out.println(setName);

        //获取构造器
        //获取指定构造器


    }
}
